package com.stx.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;//poi-3.14-20160307.jar
import org.apache.poi.ss.usermodel.Cell;//poi-3.14-20160307.jar
import org.apache.poi.ss.usermodel.Row;//poi-3.14-20160307.jar
import org.apache.poi.ss.usermodel.Sheet;//poi-3.14-20160307.jar
import org.apache.poi.ss.usermodel.Workbook;//poi-3.14-20160307.jar
import org.apache.poi.xssf.usermodel.XSSFWorkbook;//poi-ooxml-3.14-20160307.jar
import org.springframework.stereotype.Service;

import com.stx.dao.StudentDao;
import com.stx.dao.TeacherDao;
import com.stx.domain.Student;
import com.stx.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao sd;

    public List<Student> selectStudent(String building) {
        return sd.selectStudent(building);

    }

    public void updateStudent(Student student) {
        sd.updateStudent(student);
    }

    public void studentDelete(int no) {
        sd.studentDelete(no);
    }

    public List<Student> selectLived(String name) {
        return sd.selectLived(name);

    }

    public void updateDorm(Student student) {
        sd.updateDorm(student);
    }

    @Override
    public int modifyStudentPwd(int no, String newpassword) {
        return sd.modifyStudentPwd(no, newpassword);
    }

    @Override
    public Student studentLogin(int no, String pwd) {
        return sd.studentLogin(no, pwd);
    }

    @Override
    public List<Student> selectStudent() {
        // TODO Auto-generated method stub
        return sd.selectStudent();
    }

    @Override
    public Student selectStudent(int no) {
        // TODO Auto-generated method stub
        return sd.selectStudent(no);
    }

    @Override
    public void insertStudentForBatch(String headerString, InputStream is) throws IOException {
        List<Object> studentList = new ArrayList<>();
        Workbook workbook = null;
        if (headerString.endsWith(".xls")) {
            System.out.println("excel后缀为+“.xls”");
            workbook = new HSSFWorkbook(is);
        }
        if (headerString.endsWith(".xlsx")) {
            System.out.println("excel后缀为+“.xlsx”");
            workbook = new XSSFWorkbook(is);
        }
        int count = workbook.getNumberOfSheets();
        System.out.println("sheet数为：" + count);
        for (int i = 0; i < count; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            // 获取当前遍历sheet的名称
            //String sheetName = sheet.getSheetName();
/*			if (sheetName.equals("student")) {*/
            Iterator<Row> iterator = sheet.iterator();
            //这个if块是为了去掉每个sheet的标题行，之所以要判断是否存在后继元素是因为：
            //读到空的sheet时如果直接执行next()方法会报NoSuchElementException异常
            if (iterator.hasNext()) {
                iterator.next();
            }
            while (iterator.hasNext()) {//这层循环遍历sheet的行
                Row row = iterator.next();
                // System.out.println(row.getPhysicalNumberOfCells());
                List<Object> list = new ArrayList<>();
                    /*这层循环遍历每一行中的单元格，定死每行只遍历6个单元格。
					 * 将一行的单元格的值全部以字符串形式依次放入list中
					 * 若导入的excel的某个单元格没有值就当其值为""存入list。
					 * 不当成是null存入list可能是为了避免空指针异常。
					 * */
                for (int j = 0; j < 6; j++) {
                    // 设置Cell的类型，把纯数字作为String类型读进来了
                    if (row.getCell(j) != null) {
                        row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                        list.add(row.getCell(j).getStringCellValue());
                    } else {
                        list.add("");//用""代替null可能是为了避免空指针异常
                    }
                }//一行循环结束
                studentList.add(list);//list装每一列（每个学生的信息）的数据，questionList装所有学生的信息
            }//一张sheet循环结束
/*			}//if判断sheet名是否为"student"
*/
        }//整个Excel循环结束
        // 调用dao层insertStudentForBatch（）添加学生
        sd.insertStudentForBatch(studentList);
    }
}
